//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FlatVolFactory.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Models/Marketmodels/MarketModelFactory.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (Double longTermCorrelation, Double beta, Cephei::IVector<Double>^ times, Cephei::IVector<Double>^ vols, Cephei::QL::Termstructures::IYieldTermStructure^ yieldCurve, Double displacement) : CMarketModelFactory(CFlatVolFactory::typeid)
{
    CoVector<Double>^ _Ctimes;
    CoVector<Double>^ _Cvols;
    CYieldTermStructure^ _CyieldCurve;
    try
    {
#ifdef HANDLE
        _phFlatVolFactory = NULL;
#endif
        QuantLib::Real _longTermCorrelation = (QuantLib::Real)ValueHelper::Convert (longTermCorrelation);
        QuantLib::Real _beta = (QuantLib::Real)ValueHelper::Convert (beta);
        CoVector<Double>^ _Ctimes = safe_cast<CoVector<Double>^> (times);
        _Ctimes->Lock();
        INativeVector<Double>^ _NCItimes = _Ctimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtimes = safe_cast<CDoubleVector^>(_NCItimes);
        std::vector<QuantLib::Time>& _times = static_cast<std::vector<QuantLib::Time>&> (_NCtimes->GetReference ());
        CoVector<Double>^ _Cvols = safe_cast<CoVector<Double>^> (vols);
        _Cvols->Lock();
        INativeVector<Double>^ _NCIvols = _Cvols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvols = safe_cast<CDoubleVector^>(_NCIvols);
        std::vector<QuantLib::Volatility>& _vols = static_cast<std::vector<QuantLib::Volatility>&> (_NCvols->GetReference ());
        _CyieldCurve = safe_cast<CYieldTermStructure^> (yieldCurve);
        _CyieldCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldCurve->GetHandle ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement);
        _ppFlatVolFactory = new boost::shared_ptr<QuantLib::FlatVolFactory> (new QuantLib::FlatVolFactory ( _longTermCorrelation,  _beta,  _times,  _vols,  _yieldCurve,  _displacement ));
        SetMarketModelFactory (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctimes != nullptr) _Ctimes->Unlock();
        if (_Cvols != nullptr) _Cvols->Unlock();
        if (_CyieldCurve != nullptr) _CyieldCurve->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (boost::shared_ptr<QuantLib::FlatVolFactory>& childNative, Object^ owner) : CMarketModelFactory(CFlatVolFactory::typeid)
{
#ifdef HANDLE
	_phFlatVolFactory = NULL;
#endif
	_ppFlatVolFactory = &childNative;
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
}
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (QuantLib::FlatVolFactory& childNative, Object^ owner) : CMarketModelFactory(CFlatVolFactory::typeid)
{
#ifdef HANDLE
	_phFlatVolFactory = NULL;
#endif
	_ppFlatVolFactory = new boost::shared_ptr<QuantLib::FlatVolFactory> (&childNative);
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
    _FlatVolFactoryOwner = owner;
    _MarketModelFactoryOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (CFlatVolFactory^ copy) : CMarketModelFactory(CFlatVolFactory::typeid)
{
#ifdef HANDLE
	_phFlatVolFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatVolFactory = new boost::shared_ptr<QuantLib::FlatVolFactory> (copy->GetShared());
        _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (System::Type^ t) : CMarketModelFactory(CFlatVolFactory::typeid)
{
#ifdef HANDLE
	_phFlatVolFactory = NULL;
#endif
	if (!t->IsSubclassOf(CFlatVolFactory::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (QuantLib::Handle<QuantLib::FlatVolFactory>& childNative, Object^ owner)  : CMarketModelFactory(CFlatVolFactory::typeid)
{
	_phFlatVolFactory = &childNative;
	_ppFlatVolFactory = &static_cast<boost::shared_ptr<QuantLib::FlatVolFactory>>(childNative.currentLink());
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
    _FlatVolFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (QuantLib::Handle<QuantLib::FlatVolFactory> childNative)  : CMarketModelFactory(CFlatVolFactory::typeid)
{
	_phFlatVolFactory = &childNative;
	_ppFlatVolFactory = &static_cast<boost::shared_ptr<QuantLib::FlatVolFactory>>(childNative.currentLink());
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::CFlatVolFactory (QuantLib::FlatVolFactory childNative)  : CMarketModelFactory(CFlatVolFactory::typeid)
{
#ifdef HANDLE
	_phFlatVolFactory = NULL;
#endif
	_ppFlatVolFactory = new boost::shared_ptr<QuantLib::FlatVolFactory> (new QuantLib::FlatVolFactory (childNative));
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFlatVolFactory));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::~CFlatVolFactory ()
{
    if (_ppFlatVolFactory != NULL)
    {
	    delete _ppFlatVolFactory;
        _ppFlatVolFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::!CFlatVolFactory ()
{
    if (_ppFlatVolFactory != NULL)
    {
	    delete _ppFlatVolFactory;
    }
}
QuantLib::FlatVolFactory& Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::GetReference ()
{
    if (_ppFlatVolFactory == NULL) throw gcnew NativeNullException ();
	return **_ppFlatVolFactory;
}
boost::shared_ptr<QuantLib::FlatVolFactory>& Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::GetShared ()
{
    if (_ppFlatVolFactory == NULL) throw gcnew NativeNullException ();
	return *_ppFlatVolFactory;
}
QuantLib::FlatVolFactory* Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::GetPointer ()
{
    if (_ppFlatVolFactory == NULL) throw gcnew NativeNullException ();
	return &**_ppFlatVolFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatVolFactory>& Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::GetHandle ()
{
	if (_phFlatVolFactory == NULL)
	{
		_phFlatVolFactory = new Handle<QuantLib::FlatVolFactory> (*_ppFlatVolFactory);
	}
	return *_phFlatVolFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::HasNative () 
{
	return (_ppFlatVolFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IMarketModel^ Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, UInt64 numberOfFactors)
{
    CEvolutionDescription^ _Cevolution;
    try
    {
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors);
    	boost::shared_ptr<QuantLib::MarketModel> _rv = (boost::shared_ptr<QuantLib::MarketModel>)(*_ppFlatVolFactory)->create ( _evolution,  _numberOfFactors );   
        Cephei::QL::Models::Marketmodels::CMarketModel^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CMarketModel (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::IFlatVolFactory^ Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFlatVolFactory)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IFlatVolFactory^ Cephei::QL::Models::Marketmodels::Models::CFlatVolFactory_Factory::Create (Double longTermCorrelation, Double beta, Cephei::IVector<Double>^ times, Cephei::IVector<Double>^ vols, Cephei::QL::Termstructures::IYieldTermStructure^ yieldCurve, Double displacement)
{
    return gcnew CFlatVolFactory ( longTermCorrelation,  beta,  times,  vols,  yieldCurve,  displacement);
}
